import React from 'react'

import {WeaLocaleProvider, WeaTools} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

import './style'
import ContainerModle from '../../common/containerModle'
import AddExtend from './addExtend'
import SettingExtend from './settingExtend'
import SettingStyleExtend from './settingStyleExtend'
import SettingProxy from './settingProxy'
import ContainerViewExtend from './containerViewExtend'
import ContainerViewProxy from './containerViewProxy'


export default class CustomFeatures extends React.Component{

    static defaultProps = {
        javaInterface:{
            find: '/api/portalTsLogin/element/getCustomFeaturesElementData',
            save: '/api/portalTs/customFeaturesElement/save',
            update: '/api/portalTs/customFeaturesElement/update',
            saveOrUpdateItem: '/api/portalTs/customFeaturesElement/saveOrUpdateItem',
            deleteItem: '/api/portalTs/customFeaturesElement/deleteItem',
            orderItem: '/api/portalTs/customFeaturesElement/orderItem',
        },
        userJavaInterface:{
            find: '/api/portalTsLogin/element/getCustomFeaturesData',
            save: '/api/portalTs/customFeatures/save',
            delete: '/api/portalTs/customFeatures/delete',
            order: '/api/portalTs/customFeatures/order',
        }
    }

    state={
        selectedItems: [],
        noSelectedItems: [],
        stack: [],
    }

    onSaveBeforeHandler = (setting={})=>{
        setting.title = '自定义功能元素'
        setting.row = 1
        setting.col = 4
        setting.imgWidth = 75
        setting.imgHeight = 75
        setting.titleFontSize = 16
        setting.titleColor = '#000000'
        setting.rowMargin = 10
        setting.settingIcon = '/wui/theme/ecology9/image/layout/layout5/desktop-icon-default.png'

    }


    onLoadFinallyHandler = ()=>{
        this.onCustomLoad()
    }

    onCustomLoad = ()=>{
        const {id,userJavaInterface} = this.props
        WeaTools.callApi(userJavaInterface.find, 'GET', {"elementId":id}).then((result) => {
            const {data} = result
            this.setData(data)
        })
    }

    setData = (data)=>{
        if(data != null){
            for(let i in data.selectedItems){
                if(data.selectedItems.hasOwnProperty(i)){
                    data.selectedItems[i].setting = JSON.parse(data.selectedItems[i].setting)
                }

            }
            for(let i in data.noSelectedItems){
                if(data.noSelectedItems.hasOwnProperty(i)){
                    data.noSelectedItems[i].setting = JSON.parse(data.noSelectedItems[i].setting)
                }

            }
            this.setState({
                selectedItems: data.selectedItems,
                noSelectedItems: data.noSelectedItems
            })
        }
    }

    setCustomStateData = (data)=>{
        this.setState(data)
    }

    render(){

        return (
            <ContainerModle ecId={`${this && this.props && this.props.ecId || ''}_ContainerModle@vnmfsp`} {...this.props} {...(this.props.location ? this.props.location.query :{})} ContainerViewProxy={ContainerViewProxy} SettingProxy={SettingProxy} onSaveBeforeHandler={this.onSaveBeforeHandler} onLoadFinallyHandler={this.onLoadFinallyHandler} AddExtend={AddExtend} SettingExtend={SettingExtend} SettingStyleExtend={SettingStyleExtend} ContainerViewExtend={ContainerViewExtend} onCustomLoad={this.onCustomLoad} setCustomStateData={this.setCustomStateData} customState={this.state} showSharing={true}>
            </ContainerModle>
        )
    }
}